package db

import (
	"encoding/json"
	"os"
	"sync"
	"time"
)

/*
{
    "online":0,
    "offline":0,
    "warning":0,
    "notification":0,
    "AI":0,
    "lost_num":0,
    "text_to_speech":0,
    "login_num":0
}
*/

/*
	{
	    "date": "2021-08-10",
	    "today":0,
	    "yesterday":0,
	    "the_day_before_yesterday":0,
	    "three_days_before":0,
	    "four_days_before":0,
	    "five_days_before":0,
	    "six_days_before":0
	}
*/
var mu sync.Mutex

type Info_Json struct {
	Online       int `json:"online"`
	Offline      int `json:"offline"`
	Warning      int `json:"warning"`
	Notification int `json:"notification"`
	AI           int `json:"AI"`
	LostNum      int `json:"lost_num"`
	TextToSpeech int `json:"text_to_speech"`
	LoginNum     int `json:"login_num"`
}
type Date_Json struct {
	Date                  string `json:"date"`
	Today                 int    `json:"today"`
	Yesterday             int    `json:"yesterday"`
	TheDayBeforeYesterday int    `json:"the_day_before_yesterday"`
	ThreeDaysBefore       int    `json:"three_days_before"`
	FourDaysBefore        int    `json:"four_days_before"`
	FiveDaysBefore        int    `json:"five_days_before"`
	SixDaysBefore         int    `json:"six_days_before"`
}

var changes Info_Json
var Changes_channel chan Info_Json
var file_path string
var file_path2 string
var date_time string

func init() {
	date_time = time.Now().Format("2006-01-02")
	file_path = "./db/db.json"
	file_path2 = "./db/date.json"
	Changes_channel = make(chan Info_Json, 1000)
	changes = Info_Json{Online: 0, Offline: 0, Warning: 0, Notification: 0, AI: 0, LostNum: 0, TextToSpeech: 0, LoginNum: 0}
	go func() {
		for {
			time.Sleep(3 * time.Second)
			mu.Lock()
			var info_json Info_Json
			var date_json Date_Json
			json_data, err := os.ReadFile(file_path)
			if err != nil {
				panic(err)
			}
			json_data1, err2 := os.ReadFile(file_path2)
			if err2 != nil {
				panic(err2)
			}
			err = json.Unmarshal(json_data, &info_json)
			if err != nil {
				panic(err)
			}
			err = json.Unmarshal(json_data1, &date_json)
			if err != nil {
				panic(err)
			}
			if date_json.Date != date_time {
				date_json.Date = date_time
				date_json.SixDaysBefore = date_json.FiveDaysBefore
				date_json.FiveDaysBefore = date_json.FourDaysBefore
				date_json.FourDaysBefore = date_json.ThreeDaysBefore
				date_json.ThreeDaysBefore = date_json.Yesterday
				date_json.Yesterday = date_json.Today
				date_json.Today = changes.Warning

				json_data1, err2 = json.Marshal(date_json)
				if err2 != nil {
					panic(err2)
				}
				err2 = os.WriteFile(file_path2, json_data1, 0666)
				if err2 != nil {
					panic(err2)
				}
			} else {
				if changes.Warning != 0 {
					date_json.Today += changes.Warning
					json_data1, err2 = json.Marshal(date_json)
					if err2 != nil {
						panic(err2)
					}
					err2 = os.WriteFile(file_path2, json_data1, 0666)
					if err2 != nil {
						panic(err2)
					}
				}
			}

			info_json.Online += changes.Online
			info_json.Offline += changes.Offline
			info_json.Warning += changes.Warning
			info_json.Notification += changes.Notification
			info_json.AI += changes.AI
			info_json.LostNum += changes.LostNum
			info_json.TextToSpeech += changes.TextToSpeech
			info_json.LoginNum += changes.LoginNum
			changes = Info_Json{Online: 0, Offline: 0, Warning: 0, Notification: 0, AI: 0, LostNum: 0, TextToSpeech: 0, LoginNum: 0}
			json_data, err = json.Marshal(info_json)
			if err != nil {
				panic(err)
			}
			err = os.WriteFile(file_path, json_data, 0666)
			if err != nil {
				panic(err)
			}
			mu.Unlock()
		}
	}()
	go func() {
		for m := range Changes_channel {
			date_time = time.Now().Format("2006-01-02")
			changes.Online += m.Online
			changes.Offline += m.Offline
			changes.Warning += m.Warning
			changes.Notification += m.Notification
			changes.AI += m.AI
			changes.LostNum += m.LostNum
			changes.TextToSpeech += m.TextToSpeech
			changes.LoginNum += m.LoginNum
		}
	}()
}
func GetJson() Info_Json {
	mu.Lock()
	defer mu.Unlock()
	data, err := os.ReadFile(file_path)
	if err != nil {
		panic(err)
	}
	var info Info_Json
	err = json.Unmarshal(data, &info)
	if err != nil {
		panic(err)
	}
	return info
}
func Get_date_data() Date_Json {
	mu.Lock()
	defer mu.Unlock()
	data, err := os.ReadFile(file_path2)
	if err != nil {
		panic(err)
	}
	var date Date_Json
	err = json.Unmarshal(data, &date)
	if err != nil {
		panic(err)
	}
	return date
}
